﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AutoShutdownDotNet.Models;
using AutoShutdownDotNet.Actions;

namespace AutoShutdownDotNet
{
    public partial class MainForm : Form
    {

        private List<Task> Tasks = new List<Task>();

        #region Properties

        public int SelectedTaskIndex
        {
            get
            {
                if(listTasks.SelectedItems.Count > 0)
                    return listTasks.SelectedIndices[0];
                return -1;
            }
        }

        public int SelectedTaskId
        {
            get
            {
                if (SelectedTaskIndex > -1)
                    return Tasks[SelectedTaskIndex].Id;
                return -1;
            }
        }

        public bool BusyOnWork { get; set; }

        #endregion

        #region Methods

        private void ShowTasks()
        {
            listTasks.Items.Clear();
            foreach (Task task in Tasks)
            {
                ListViewItem item = new ListViewItem(task.Title);
                item.Tag = task.Id;
                item.SubItems.Add(Task.ActionToString(task.Action));
                item.SubItems.Add(task.WhenToString());
                item.SubItems.Add(task.Time);
                item.SubItems.Add(task.CreatedDate.ToShortDateString());
                listTasks.Items.Add(item);
            }
        }

        private void CheckForUpcomeTasks()
        {
            DateTime now = DateTime.Now;
            foreach (Task task in Tasks)
            {
                if (task.Active && ((task.When.HasValue && task.When.Value == now) || task.When == null))
                {
                    if(task.Hour == now.Hour && task.Minute == now.Minute)
                    {
                        BusyOnWork = true;
                        if(task.AskBefore)
                        {
                            DoTaskForm doTaskForm = new DoTaskForm(task);
                            if(doTaskForm.ShowDialog() == DialogResult.Cancel)
                            {
                                task.Active = false;
                                BusyOnWork = false;
                            }
                        }
                        else
                        {
                            BusyOnWork = true;
                            DoAction doAction = new DoAction(task.Action);
                            if (task.Action == AutoShutdownAction.RunApplication)
                                doAction.FilePath = task.FilePath;
                            else if (task.Action == AutoShutdownAction.ShowMessage)
                                new MessageForm(task.Title, task.Message).ShowDialog();
                            doAction.Do();
                            BusyOnWork = false;
                        }
                    }
                }
            }
        }

        private bool _firstTimeTimerStarts = true;
        private void SetTimer()
        {
            DateTime now = DateTime.Now;
            if (now.Second == 0)
                timer.Interval = 60000;
            else
                timer.Interval = 60000 - (now.Second*1000);
            ShowDateAndTime();
        }

        private void ShowDateAndTime()
        {
            lblDateAndTime.Text = DateTime.Now.ToShortDateString() + ", Time is " + DateTime.Now.Hour.ToString() + ":" +
                DateTime.Now.Minute.ToString();
        }

        #endregion

        public MainForm()
        {
            InitializeComponent();
            SetTimer();
            Tasks = Task.Select(null, null);
            ShowTasks();
        }

        private void listTasks_ItemActivate(object sender, EventArgs e)
        {
            if(SelectedTaskIndex > -1)
            {
                TaskForm taskForm = new TaskForm(Convert.ToInt32(SelectedTaskId));
                if(taskForm.ShowDialog() == DialogResult.OK)
                {
                    listTasks.SelectedItems[0].Text = taskForm.Title;
                    listTasks.SelectedItems[0].SubItems[1].Text = Task.ActionToString(taskForm.Action);
                    listTasks.SelectedItems[0].SubItems[2].Text = Task.WhenToString(taskForm.When);
                    listTasks.SelectedItems[0].SubItems[3].Text = Task.TimeToString(taskForm.Hour,taskForm.Minute);
                    Tasks[SelectedTaskIndex].Title = taskForm.Title;
                    Tasks[SelectedTaskIndex].Action = taskForm.Action;
                    Tasks[SelectedTaskIndex].Active = taskForm.Active;
                    Tasks[SelectedTaskIndex].AskBefore = taskForm.AskBeforeAnyAction;
                    Tasks[SelectedTaskIndex].Comment = taskForm.Comment;
                    Tasks[SelectedTaskIndex].FilePath = taskForm.FilePath;
                    Tasks[SelectedTaskIndex].Hour = taskForm.Hour;
                    Tasks[SelectedTaskIndex].Minute = taskForm.Minute;
                    Tasks[SelectedTaskIndex].Timer = taskForm.Timer;
                    Tasks[SelectedTaskIndex].When = taskForm.When;
                }
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            ShowDateAndTime();
            if (_firstTimeTimerStarts)
                timer.Interval = 60000;
            if(!BusyOnWork)
                CheckForUpcomeTasks();
            _firstTimeTimerStarts = false;
        }

        private void NewTask(AutoShutdownAction action)
        {
            TaskForm taskForm = new TaskForm();
            taskForm.Action = action;
            if (taskForm.ShowDialog() == DialogResult.OK)
            {
                Tasks.Add(taskForm.ResultTask);
                ListViewItem item = new ListViewItem(taskForm.ResultTask.Title);
                item.Tag = taskForm.ResultTask.Id;
                item.SubItems.Add(Task.ActionToString(taskForm.ResultTask.Action));
                item.SubItems.Add(taskForm.ResultTask.WhenToString());
                item.SubItems.Add(taskForm.ResultTask.Time);
                item.SubItems.Add(taskForm.ResultTask.CreatedDate.ToShortDateString());
                listTasks.Items.Add(item);
            }
        }

        private void mnuNewTask_Click(object sender, EventArgs e)
        {
            NewTask(AutoShutdownAction.PowerOff);
        }

        private void mnuNewAutoShutdown_Click(object sender, EventArgs e)
        {
            NewTask(AutoShutdownAction.PowerOff);
        }

        private void mnuNewAutoReboot_Click(object sender, EventArgs e)
        {
            NewTask(AutoShutdownAction.Reboot);
        }

        private void mnuNewAutoLogoff_Click(object sender, EventArgs e)
        {
            NewTask(AutoShutdownAction.Logoff);
        }

        private void mnuNewAutoStandby_Click(object sender, EventArgs e)
        {
            NewTask(AutoShutdownAction.Standby);
        }

        private void mnuNewAutoHibernate_Click(object sender, EventArgs e)
        {
            NewTask(AutoShutdownAction.Hibernate);
        }

        private void mnuNewAutoLock_Click(object sender, EventArgs e)
        {
            NewTask(AutoShutdownAction.Lock);
        }

        private void mnuNewAutoRun_Click(object sender, EventArgs e)
        {
            NewTask(AutoShutdownAction.RunApplication);
        }
    }
}
